package com.macro.mall.portal.service;

import com.macro.mall.portal.domain.MongoProduct;
import com.macro.mall.portal.repository.MongoProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MongoProductService {
    @Autowired
    private MongoProductRepository productRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MongoHealthCheckService healthCheckService;

    // 测试连接
    public boolean checkMongoConnection() {
        return healthCheckService.checkMongoHealth();
    }

    public String testConnection() {
        return healthCheckService.testConnection();
    }

    // ========== 查询操作 ==========

    // 查询所有产品
    public List<MongoProduct> findAllProducts() {
        return productRepository.findAll();
    }

    // 根据ID查询
    public MongoProduct findProductById(String id) {
        return productRepository.findById(id).orElse(null);
    }

    // 条件查询
    public List<MongoProduct> findProductsByCategory(String category) {
        return productRepository.findByCategory(category);
    }

    // 复杂查询
//    public List<Product> searchProducts(String name, BigDecimal maxPrice, String category) {
//        Query query = new Query();

//        if(name != null && !name.isEmpty()) {
//            query.addCriteria(Criteria.where("name").regex(name, "i"));
//        }
//
//        if(maxPrice != null) {
//            query.addCriteria(Criteria.where("price").lte(maxPrice));
//        }
//
//        if(category != null && !category.isEmpty()) {
//            query.addCriteria(Criteria.where("category").is(category));
//        }
//
//        return mongoTemplate.find(query, Product.class);
//        return Product.class;
//    }

    // ========== 增加操作 ==========

    // 添加单个产品
    public MongoProduct addProduct(MongoProduct product) {
        return productRepository.save(product);
    }

    // 批量添加
    public List<MongoProduct> addProducts(List<MongoProduct> products) {
        return productRepository.saveAll(products);
    }

    // ========== 删除操作 ==========

    // 根据ID删除
    public void deleteProductById(String id) {
        productRepository.deleteById(id);
    }

    // 根据条件删除
//    public long deleteProductsByCategory(String category) {
//        Query query = new Query(Criteria.where("category").is(category));
//        return mongoTemplate.remove(query, Product.class).getDeletedCount();
//    }

    // 删除所有
    public void deleteAllProducts() {
        productRepository.deleteAll();
    }



    // 2. 按名称模糊查询
    public List<MongoProduct> searchProductsByName(String keyword) {
        return productRepository.findByName(keyword);
    }

    // 3. 按分类精确查询
    public List<MongoProduct> getProductsByCategory(String category) {
        return productRepository.findByCategory(category);
    }

    // 4. 插入/更新数据（自动判断ID是否存在）
    public MongoProduct saveProduct(MongoProduct product) {

        return productRepository.save(product); // ID存在则更新，不存在则插入
    }

    // 5. 按名称删除数据
    public List<MongoProduct> deleteProductsByName(String name) {
        return productRepository.deleteByName(name);
    }

}
